package com.example.guardian.home;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.guardian.MainActivity;
import com.example.guardian.R;
import com.example.guardian.anticheatguard.AntiCheatGuardFragment;
import com.example.guardian.network.PostKeywords;

import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class PhoneActivity extends AppCompatActivity {
    private String type;
    private String number;
    private String zone;
    private String company;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_phone);
        ActionBar actionBar = getSupportActionBar();
        SearchView searchView = findViewById(R.id.phone_search);
        TextView number_is_view = findViewById(R.id.number_is);
        TextView type_view = findViewById(R.id.number_type);
        TextView number_view = findViewById(R.id.number);
        TextView zone_view = findViewById(R.id.number_zone);
        TextView company_view = findViewById(R.id.number_company);
        if (actionBar != null){
            actionBar.hide();
        }
        Button back_button = findViewById(R.id.phone_back_btn);
        back_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PhoneActivity.this.finish();

            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                String telephone_num = s;
                if(s.length()<7|s.length()>11){
                    Toast.makeText(getApplicationContext(),"号码长度需要大于等于7位小于等于11位",Toast.LENGTH_SHORT).show();
                }else {
                    PostKeywords.sendOkHttpRequest("http://124.220.154.99:5000/tel_num_select", s, new okhttp3.Callback() {
                        @RequiresApi(api = Build.VERSION_CODES.N)
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            //得到服务器返回的具体内容
                            String responseData = response.body().string();
                            //initTCase();

                            showResponse(responseData);
                        }

                        @Override
                        public void onFailure(Call call, IOException e) {
                            //在这里对异常数据进行处理
                        }

                        private void showResponse(final String response) {
                            runOnUiThread(new Runnable() {
                                @RequiresApi(api = Build.VERSION_CODES.N)
                                @Override
                                public void run() {
                                    if (response.isEmpty()) {
                                        Toast.makeText(getApplicationContext(), "请输入内容", Toast.LENGTH_SHORT).show();
                                    } else {
                                        parseDiffJson(response);
                                        number_is_view.setText("该号码为");
                                        type_view.setText(type);
                                        number_view.setText(number);
                                        zone_view.setText(zone);
                                        company_view.setText(company);
                                    }
                                }
                            });
                        }
                    });
                }
                return false;

                }


            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
    }
    //解析JSON
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void parseDiffJson(String json) {
        try {
            JSONObject jsonArray = new JSONObject(json);
            number = jsonArray.getString("phone");
            zone = jsonArray.getString("province")+jsonArray.getString("city");
            company = jsonArray.getString("phone_type");
            company = jsonArray.getString("phone_type");
            type = jsonArray.getString("type");
            Log.d("1", type);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}